<template>
  <el-menu class="menu" :default-active="defaultActive" unique-opened router>
    <template v-for="menu in menuList">
      <!-- 目录-有多个子元素 -->
      <el-submenu
        v-if="menu.meta && menu.meta.type === 0"
        :key="menu.path"
        :index="menu.path"
      >
        <template slot="title">
          <i v-if="menu.meta && menu.meta.icon" :class="menu.meta.icon"></i>
          <span>{{ menu.meta.title }}</span>
        </template>

        <el-menu-item
          v-for="child in menu.children"
          :key="child.path"
          :index="child.path"
        >
          <i v-if="child.meta && child.meta.icon" :class="child.meta.icon"></i>
          <span slot="title">{{ child.meta.title }}</span>
        </el-menu-item>
      </el-submenu>

      <!-- TODO: 目录-只有一个子元素 -->

      <!-- 外链 -->
      <el-menu-item
        v-if="menu.meta && menu.meta.type === 1"
        :key="menu.path"
        :index="menu.path"
      >
        <i v-if="menu.meta && menu.meta.icon" :class="menu.meta.icon"></i>
        <span slot="title">{{ menu.meta.title }}</span>
      </el-menu-item>
    </template>
  </el-menu>
</template>

<script>
import { mapState } from "vuex";

export default {
  name: "Menu",
  data() {
    return {};
  },
  computed: {
    ...mapState(['menuList']),
    defaultActive() {
      const { path = "/" } = this.$route;

      return path;
    },
  },
};
</script>

<style lang="scss" scoped></style>
